/**
 * 附加参数配置相关
 */
import {
  getReportParam, // 附加参数列表
  deleteReportParam,
  editReportParam, // 附加参数修改
  addReportParam, // 附加参数添加
} from '@/api/websheet.js'

export const FjcsMixin = {
  data() {
    return {
      paramList: [], //附加参数列表
      newParamList: [], //本地附加参数列表
      coordinate: {}, //审批人坐标/模板初始化坐标--单选区域坐标
      persionList: [
        {
          bbjs: '填表人',
          title: '填表人',
          rymc: '',
          bz: '',
          bbdm: '',
        },
        {
          bbjs: '复核人',
          title: '复核人',
          rymc: '',
          bz: '',
          bbdm: '',
        },
        {
          bbjs: '负责人',
          title: '负责人',
          rymc: '',
          bz: '',
          bbdm: '',
        },
      ], //审批人列表
    }
  },
  created() {
  },
  methods: {
    // 附加参数设置保存
    handleParam(record) {
      if (
        record.csmc &&
        record.csz &&
        record.lx &&
        record.hh !== null &&
        record.hh !== '' &&
        record.hh !== undefined &&
        record.lh !== null &&
        record.lh !== '' &&
        record.lh !== undefined
      ) {
        if (record.id) {
          editReportParam(record).then((res) => {
            if (res.code === 200) {
              this.queryReportParam()
              this.$message.success('保存成功')
            } else {
              this.$message.warning('服务器出错!')
            }
          })
        } else {
          addReportParam(record).then((res) => {
            if (res.code === 200) {
              this.queryReportParam()
              this.$message.success('保存成功')
            } else {
              this.$message.warning('服务器出错!')
            }
          })
        }
      } else {
        this.$message.warning('信息未填全')
      }
    },
    // 附加参数配置
    queryReportParam(bbdm) {
      var a = this.parameter&&this.parameter.bbpc;
      var r=a.replace(/^(\d{4})(\d{2})(\d{2})$/, "$1-$2-$3");
      getReportParam({ bbdm: bbdm || this.exportJson.bbdm
        , bbpc: r
        , bbdms: this.parameter&&this.parameter.bbdmS }).then((res) => {
        if (res.code === 200) {
          let list = res.result.records || []
          this.paramList = list
          //  获取附加参数配置渲染到Excel
          if (list.length > 0) {
            setTimeout(() => {
              list.map((item, i) => {
                if (
                  (typeof item.hh === 'number' || (typeof item.hh === 'string' && item.hh !== '')) &&
                  (typeof item.lh === 'number' || (typeof item.lh === 'string' && item.lh !== ''))
                ) {
                  window.luckysheet.setCellValue(
                    item.hh - 0,
                    item.lh - 0,
                    {
                      v: item.csz,
                      m: item.csz,
                      persionInfo: item,
                    },
                    {
                      isRefresh: i === list.length - 1,
                    }
                  )
                }
              })
            }, 500)
          }
        } else {
          this.$message.warning('服务器出错!')
        }
      })
    },
    // 删除坐标
    deleteCoordinate(record, listType) {
      if (listType === 'persionList') {
        this[listType] = this[listType].map((item) => {
          if (listType === 'persionList' && record.bbjs === item.bbjs) {
            item.hh = ''
            item.lh = ''
          }
          return item
        })
      } else {
        record.hh = ''
        record.lh = ''
      }
    },
    // 设置坐标
    setCoordinate(record, listType) {
      if (
        this.coordinate.r &&
        this[listType].some((item) => item.hh === this.coordinate.r && item.lh === this.coordinate.c)
      ) {
        this.$message.warning('坐标已被使用')
        return
      }
      if (listType === 'persionList') {
        this[listType] = this[listType].map((item) => {
          if (listType === 'persionList' && record.bbjs === item.bbjs) {
            item.hh = this.coordinate.r
            item.lh = this.coordinate.c
          }
          return item
        })
      } else {
        record.hh = this.coordinate.r
        record.lh = this.coordinate.c
      }
    },
    //附加参数配置删除
    deleteCspz(record) {
      deleteReportParam({ id: record.id }).then((res) => {
        if (res.code === 200) {
          this.$message.success('删除成功')
          this.queryReportParam()
        } else {
          this.$message.warning('服务器出错!')
        }
      })
    },
  }
}